package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SemestersView extends Activity{
	public Calendar date;
	private String[][] aboutSemesters = null;
//	private String semesterName = "";
//	private String ActivityDescription = "";
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.semesters);
	    
	    updateListValues();
	    ListView activities = (ListView)findViewById(R.id.listViewSemesters);
	    activities.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,
	    		int position, long id) {
	    		showSemester(aboutSemesters[position][0], aboutSemesters[position][1],
	    				aboutSemesters[position][2],aboutSemesters[position][3]);
	    	}
	    });
	}

	private void updateListValues() {
	    ListView activities = (ListView)findViewById(R.id.listViewSemesters);

	    BD bd = new BD();
	    ArrayList<String> activi = bd.getSemesterOrderByEndDate(this);
	    
		aboutSemesters = new String[activi.size()][4];
	    for (int i = 0; i < activi.size(); i++) {
			String[] date = activi.get(i).split(" ");
			aboutSemesters[i][0] = date[0];
			aboutSemesters[i][1] = date[1];
			aboutSemesters[i][2] = date[2];
			aboutSemesters[i][3] = date[3];
		}
	    
	    String[] values = new String[aboutSemesters.length];
	    for (int i = 0; i < values.length; i++) {
	        float mean = bd.calculateSemesterMean(this, aboutSemesters[i][0]);
            String grade= "";
	        if (mean > 0){
                grade = String.valueOf(mean);
            }
			values[i] = aboutSemesters[i][0]+ "\t\t" + grade;
		}
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    activities.setAdapter(adapter);
	    activities.invalidate();
	}

	
	private void showSemester(final String name, final String initialDate, final String endDate, final String mean) {
		Calendar date = Calendar.getInstance();

		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        int today = date.get(Calendar.DATE);
		Intent trocatela = new Intent(SemestersView.this, SingleSemesterView.class);
	    trocatela.putExtra("date", year+"-"+month+"-"+today);
	    trocatela.putExtra("semesterName", name);
	    trocatela.putExtra("initialDate", initialDate);
	    trocatela.putExtra("endDate", endDate);
	    trocatela.putExtra("mean", mean);
	    SemestersView.this.startActivity(trocatela);
	    SemestersView.this.finish();
		
		}
	
	@Override
	public void onBackPressed() {
		
        back();	        		 
        return;
	}
	
	private void back() {
		Calendar date = Calendar.getInstance();

		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        int today = date.get(Calendar.DATE);
        
        Intent trocatela = new Intent(SemestersView.this, CalendarView.class);
    	trocatela.putExtra("date", year+"-"+month+"-"+today);
    	
    	SemestersView.this.startActivity(trocatela);
    	SemestersView.this.finish();	 
	}

    private void addSemester() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        
        Intent intent = new Intent(SemestersView.this,AddSemestersView.class);
        intent.putExtra("date", year+"-"+month+"-"+day);
        SemestersView.this.startActivity(intent);
        SemestersView.this.finish();
    }

    private void addVacation() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        
        Intent intent = new Intent(SemestersView.this,VacationsView.class);
        intent.putExtra("date", year+"-"+month+"-"+day);
        SemestersView.this.startActivity(intent);
        SemestersView.this.finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     super.onCreateOptionsMenu(menu);
     menu.add(0,0,0,"Add semester");
     menu.add(1,1,1,"Add vacation");
     return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
         switch (item.getItemId()) {
            case 0:
                addSemester();
                break;
            case 1:
                addVacation();
                break;
         }
         return false;
    } 
}
